home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / buildlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  3.7 KB  |  131 lines

  1. ;/* buildlist.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 buildlist.c
  3. Blink FROM LIB:c.o,buildlist.o TO buildlist LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. ** The code below demonstrates the concepts and functions discussed in this
  7. ** chapter by building an application-defined Exec List.
  8. **
  9. ** buildlist.c - example which uses an application-specific Exec list
  10. */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/lists.h>
  14. #include <exec/nodes.h>
  15. #include <exec/memory.h>
  16.  
  17. #include <clib/alib_protos.h>
  18. #include <clib/exec_protos.h>
  19.  
  20. #include <string.h>
  21. #include <stdio.h>
  22.  
  23. #ifdef LATTICE
  24. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  25. void chkabort(void) { return; }  /* really */
  26. #endif
  27.  
  28. /* Our function prototypes */
  29. VOID AddName(struct List *, UBYTE *);
  30. VOID FreeNameNodes(struct List *);
  31. VOID DisplayNameList(struct List *);
  32. VOID DisplayName(struct List *, UBYTE *);
  33.  
  34. struct NameNode {
  35.     struct Node nn_Node;        /* System Node structure */
  36.     UBYTE  nn_Data[62];         /* Node-specific data */
  37. };
  38.  
  39. #define NAMENODE_ID   100       /* The type of "NameNode" */
  40.  
  41. VOID main(VOID)
  42. {
  43.     struct  List    *NameList;    /* Note that a MinList would also work */
  44.  
  45.     if (!( NameList = AllocMem(sizeof(struct List),MEMF_CLEAR)) )
  46.         printf("Out of memory\n");
  47.     else {
  48.         NewList(NameList);        /* Important: prepare header for use */
  49.  
  50.         AddName(NameList,"Name7");   AddName(NameList,"Name6");
  51.         AddName(NameList,"Name5");   AddName(NameList,"Name4");
  52.         AddName(NameList,"Name2");   AddName(NameList,"Name0");
  53.  
  54.         AddName(NameList,"Name7");   AddName(NameList,"Name5");
  55.         AddName(NameList,"Name3");   AddName(NameList,"Name1");
  56.  
  57.         DisplayName(NameList,"Name5");
  58.         DisplayNameList(NameList);
  59.  
  60.         FreeNameNodes(NameList);
  61.         FreeMem(NameList,sizeof(struct List));  /* Free list header */
  62.     }
  63. }
  64.  
  65. /* Allocate a NameNode structure, copy the given name into the structure,
  66.  * then add it the specified list.  This example does not provide an
  67.  * error return for the out of memory condition.
  68. */
  69. VOID AddName(struct List *list, UBYTE *name)
  70. {
  71.     struct NameNode *namenode;
  72.  
  73.     if (!( namenode = AllocMem(sizeof(struct NameNode),MEMF_CLEAR) ))
  74.         printf("Out of memory\n");
  75.     else {
  76.         strcpy(namenode->nn_Data,name);
  77.         namenode->nn_Node.ln_Name = namenode->nn_Data;
  78.         namenode->nn_Node.ln_Type = NAMENODE_ID;
  79.         namenode->nn_Node.ln_Pri  = 0;
  80.         AddHead((struct List *)list,(struct Node *)namenode);
  81.     }
  82. }
  83.  
  84. /*
  85.  * Free the entire list, including the header.  The header is not updated
  86.  * as the list is freed.  This function demonstrates how to avoid
  87.  * referencing freed memory when deallocating nodes.
  88.  */
  89. VOID FreeNameNodes(struct List *list)
  90. {
  91.     struct NameNode *worknode;
  92.     struct NameNode *nextnode;
  93.  
  94.     worknode = (struct NameNode *)(list->lh_Head); /* First node */
  95.     while (nextnode = (struct NameNode *)(worknode->nn_Node.ln_Succ)) {
  96.         FreeMem(worknode,sizeof(struct NameNode));
  97.         worknode = nextnode;
  98.     }
  99.  
  100. }
  101.  
  102. /*
  103.  * Print the names of each node in a list.
  104.  */
  105. VOID DisplayNameList(struct List *list)
  106. {
  107. struct Node *node;
  108.  
  109.     if (list->lh_TailPred == (struct Node *)list)
  110.         printf("List is empty.\n");
  111.     else {
  112.         for (node = list->lh_Head ; node->ln_Succ ; node = node->ln_Succ)
  113.             printf("%lx -> %s\n",node,node->ln_Name);
  114.     }
  115. }
  116.  
  117. /*
  118.  * Print the location of all nodes with a specified name.
  119.  */
  120. VOID DisplayName(struct List *list, UBYTE *name)
  121. {
  122. struct Node *node;
  123.  
  124.     if (node = FindName(list,name)) {
  125.         while (node) {
  126.             printf("Found a %s at location %lx\n",node->ln_Name,node);
  127.             node = FindName((struct List *)node,name);
  128.         }
  129.     } else printf("No node with name %s found.\n",name);
  130. }
  131.